﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Newtonsoft.Json;
using Common;

namespace Habito.Admini
{
	public partial class ReferenceEdit : System.Web.UI.Page
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			if (string.IsNullOrEmpty(Ticket.UserName) || Session["username"] == null)
			{
				Response.Redirect("Login.aspx");
			}
			ViewState["id"] = Request.QueryString["id"];
		}
		protected override void OnInit(EventArgs e)
		{

			if (Request.QueryString["GetReferenceByID"] == "1")
			{
				int id = int.Parse(Request.QueryString["id"].Replace("\'", ""));
				Ajax.BLL.References bll = new Ajax.BLL.References();
				Ajax.Model.References model = bll.GetModel(id);
				Response.Write(JsonConvert.SerializeObject(model));
				Response.End();
			}


			if (Request.QueryString["SaveReference"] == "1")
			{
				int id = int.Parse(Request.QueryString["id"].Replace("\'", ""));
				int year = int.Parse(Request.Params["year"].Replace("\'", ""));
				string projectName = Request.Params["projectName"];
				string city = Request.Params["city"];
				string description = Request.Params["description"]; 
				Ajax.BLL.References bll = new Ajax.BLL.References();
				Ajax.Model.References model = new Ajax.Model.References(); 
				model.ID = id;
				model.City = city;
				model.Description = description;
				model.ProjectName = projectName;
				model.RYear = year;

				if (model.ID == 0)
				{
					Response.Write(bll.Add(model));
					Response.End();
				}
				else
				{
					Response.Write(bll.Update(model));
					Response.End();
				}

				Response.End();
			}



			base.OnInit(e);
		}
	}
}